package com.lailai.domainservice.response;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.slf4j.MDC;

/**
 * @Author lgr
 * @Date 2025/9/15
 * @Email 983105834@qq.com
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class SingleResponse<T> extends Response {
    private T data;

    public static <T> SingleResponse<T> of(T data) {
        SingleResponse<T> response = new SingleResponse<T>();
        // 获取MDC中绑定的traceId
        String traceId = MDC.get("traceId");
        response.setTraceId(traceId);
        response.setSuccess(true);
        response.setData(data);
        return response;
    }

    public static <T> SingleResponse<T> ofFailure(String errCode, String errMessage) {
        SingleResponse<T> response = new SingleResponse<T>();
        String traceId = MDC.get("traceId");
        response.setTraceId(traceId);
        response.setSuccess(false);
        response.setErrCode(errCode);
        response.setErrMessage(errMessage);
        return response;
    }

    public T getData() {
        return this.data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public String toString() {
        return super.toString() + "SingleResponse{data=" + this.data + '}';
    }
}
